﻿using Models;
using MySqlSugar;
using Repositories.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Repositories.Repository
{
    public class ConfigRepository : IConfigRepository
    {
        protected readonly SqlSugarClient _db;
        public ConfigRepository()
        {
            _db = SugarDao.GetInstance();
        }

        #region 查询

        /// <summary>
        /// 查询单条根据主键
        /// </summary>
        public ConfigModel GetById(int? id)
        {
            var model = _db.Queryable<ConfigModel>().InSingle(id);
            return model;
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        public Queryable<ConfigModel> GetList()
        {
            return _db.Queryable<ConfigModel>();
        }

        /// <summary>
        /// 根据条件查询
        /// </summary>
        public Queryable<ConfigModel> GetList(Expression<Func<ConfigModel, bool>> where)
        {
            return _db.Queryable<ConfigModel>().Where(where);
        }

        /// <summary>
        /// 按页查询 OrderFields string 支持复杂【id desc,name asc】
        /// </summary>
        public Queryable<ConfigModel> GetPageList(Expression<Func<ConfigModel, bool>> where, int pageIndex, int pageSize, string OrderFields)
        {
            switch (OrderFields)
            {
                default:
                    return _db.Queryable<ConfigModel>().Where(where).OrderBy(it => it.Id, OrderByType.desc).Skip((pageIndex - 1) * pageSize).Take(pageSize);

            }
        }
#endregion

        #region 删除
        /// <summary>
        /// 根据主键删除
        /// </summary>
        public bool DeleteById(int id)
        {
            return _db.Delete<ConfigModel, int>(id);
        }

        /// <summary>
        /// 根据主键组批量删除
        /// </summary>
        public bool DeleteById(int[] ids)
        {
            return _db.Delete<ConfigModel, int>(ids);
        }

        /// <summary>
        /// 根据实体删除【一定要带主键！！！】
        /// </summary>
        public bool DeleteByModel(ConfigModel model)
        {
            return _db.Delete(model);
        }

        #endregion

        #region 新增
        /// <summary>
        /// 强制新增，不管主键
        /// </summary>
        public int Insert(ConfigModel model)
        {
            return int.Parse(_db.Insert(model).ToString());
        }

        /// <summary>
        /// 批量插入 适合海量数据插入
        /// </summary>
        public bool InsertRange(List<ConfigModel> list)
        {
            return _db.SqlBulkCopy(list);
        }

        #endregion

        #region 修改
        /// <summary>
        /// 根据实体更新【主键一定要有！！！】
        /// </summary>
        public bool Update(ConfigModel model)
        {
            return _db.Update(model);
        }

        /// <summary>
        /// 10条以上批量更新
        /// </summary>
        public bool UpdateRange(List<ConfigModel> list)
        {
            return _db.SqlBulkReplace(list);
        }
        #endregion
    }
}

